package com.cictec.middleware.tde.rm.disp.receiver.handler;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.feign.distribute.MiddlewareBusApplyService;
import com.cictec.middleware.feign.distribute.MiddlewareBusWarnService;
import com.cictec.middleware.tde.rm.disp.annotation.HandleMessages;
import com.cictec.middleware.tde.rm.disp.receiver.AbstractReceiverHandler;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x04TerminalMessage;

/**
 * 0x04 上传短语
 * 
 * @author Administrator
 *
 */
@Component
@HandleMessages(0x04)
public class TerminalUploadMessageHandler extends AbstractReceiverHandler {

	private static Logger logger = LoggerFactory.getLogger(TerminalUploadMessageHandler.class);

	@Autowired
	private MiddlewareBusWarnService middlewareBusWarnService;
	@Autowired
	private MiddlewareBusApplyService middlewareBusApplyService;

	@Override
	public void handleMessage(TerminalMessage message, IoSession session) {

		Rm0x04TerminalMessage msg = (Rm0x04TerminalMessage) message;
		// 0x80 指令应答 收到需要应答的数据包
		sendMessageToTerminal(createResponse(message), session);

		logger.debug("【0x04  上传短语】终端 {} Session {} 消息信息 {}  短消息编号  {}", message.getTerminalId(), message.getSessionId(),
				msg.getMessageType(), msg.getMessageNumber());

		if (msg.getMessageNumber() == 0x79 && msg.getMessageType() == 0x03) {// 手动报警
			middlewareBusWarnService.uploadManualWarn(message.getTerminalId(), msg.getLat(), msg.getLng(), msg.getSpeed(), msg.getCreateTime());

		// } else if (tum.getMessageType() == 0x00) {// 调度申请信息
		} else {
			middlewareBusApplyService.uploadDispatchApply(message.getTerminalId(), msg.getMessageNumber(), msg.getCreateTime());
		}
	}

}
